package mymac.com.weather;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Calendar;

/**
 * Created by mymac on 2017/3/6.
 */

public class StartActivity extends Activity {
    private EditText editText;
    private Button button;
    private ArrayList<Future> futures = new ArrayList<>();
    private Activity activity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        editText = (EditText) findViewById(R.id.城市名称);
        button = (Button) findViewById(R.id.查找);
        activity = this;
        button.setOnClickListener(new MyOnClickListener());

    }

    private class MyOnClickListener implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            String cityName = editText.getText().toString().trim();
            String key = "17e5729f2b6a2b1b2dd0327ab7007a51";
            final String address = "http://v.juhe.cn/weather/index?cityname=" + cityName + "&key=" + key;
            HttpRequest.getInstance().getRequest(address, new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    JSONObject json = (JSONObject) msg.obj;
                    Log.e("-----json----->", json.toString());
                    getWeatherData(json);

                    if (Weather.getInstance().getResultcode().equals("200")) {
                        Toast.makeText(StartActivity.this, "" + Weather.getInstance().getReason(), Toast.LENGTH_SHORT).show();
                        JSONObject resultJson = json.optJSONObject("result");
                        Log.e("-----resultJson---->", resultJson.toString());

                        JSONObject sk = resultJson.optJSONObject("sk");
                        Log.e("--------sk-------->", sk.toString());
                        getSkData(sk);

                        JSONObject today = resultJson.optJSONObject("today");
                        Log.e("-------today------->", today.toString());
                        getTodayData(today);

                        JSONObject futureJson = resultJson.optJSONObject("future");
                        Log.e("-----futureJson----->", futureJson.toString());
                        String date = Today.getInstance().getDate_y();

                        String[] year = date.split("年");
                        String[] month = year[1].split("月");
                        String[] day = month[1].split("日");

                        Calendar cal = Calendar.getInstance();
                        int y = cal.get(Calendar.YEAR);
                        Log.e("------year------>","" + y);
                        int m = cal.get(Calendar.MONTH);
                        Log.e("------month----->","" + m);
                        int d = cal.get(Calendar.DAY_OF_MONTH);
                        Log.e("------day------->","" + d);

                        int result = Integer.parseInt(year[0] + month[0] + day[0]);
                        System.out.println(result);
                        for (int i = 1; i <= 5; i++) {
                            if (m < Integer.parseInt(day[0])){

                            }
                            result = result + 1;
                            JSONObject dateFuture = futureJson.optJSONObject("day_" + result);
                            Future future = new Future();
                            future.date = dateFuture.optString("date");
                            future.temperature = dateFuture.optString("temperature");
                            future.weather = dateFuture.optString("weather");
                            future.wind = dateFuture.optString("wind");
                            future.week = dateFuture.optString("week");
                            futures.add(future);
                        }
                        System.out.println(futures.size());
                        Result.getInstance().futures = futures;
                        Intent intent = new Intent(StartActivity.this, MainActivity.class);
                        startActivity(intent);
                        finish();
                    } else {
                        Toast.makeText(StartActivity.this, "" + Weather.getInstance().getReason(), Toast.LENGTH_SHORT).show();
                    }
                }

            });

        }

    }


    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            if((System.currentTimeMillis()-exitTime) > 2000){
                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private static void getWeatherData(JSONObject jsonObject){
        Weather.getInstance().resultcode = jsonObject.optString("resultcode");
        Weather.getInstance().reason = jsonObject.optString("reason");
        Weather.getInstance().error_code = jsonObject.optString("error_code");
    }

    private static void getTodayData(JSONObject jsonObject) {
        Today.getInstance().city = jsonObject.optString("city");
        Today.getInstance().comfort_index = jsonObject.optString("comfort_index");
        Today.getInstance().temperature = jsonObject.optString("temperature");
        Today.getInstance().weather = jsonObject.optString("weather");
        Today.getInstance().wind = jsonObject.optString("wind");
        Today.getInstance().week = jsonObject.optString("week");
        Today.getInstance().date_y = jsonObject.optString("date_y");
        Today.getInstance().dressing_index = jsonObject.optString("dressing_index");
        Today.getInstance().dressing_advice = jsonObject.optString("dressing_advice");
        Today.getInstance().uv_index = jsonObject.optString("uv_index");
        Today.getInstance().wash_index = jsonObject.optString("wash_index");
        Today.getInstance().travel_index = jsonObject.optString("travel_index");
        Today.getInstance().exercise_index = jsonObject.optString("exercise_index");
        Today.getInstance().drying_index = jsonObject.optString("drying_index");
        //Today.getInstance().weather_id.fa = jsonObject.optJSONObject("weather_id").optString("fa");
        //Today.getInstance().weather_id.fb = jsonObject.optJSONObject("weather_id").optString("fb");
    }

    private static void getSkData(JSONObject jsonObject) {
        Sk.getInstance().temp = jsonObject.optString("temp");
        Sk.getInstance().humidity = jsonObject.optString("humidity");
        Sk.getInstance().time = jsonObject.optString("time");
        Sk.getInstance().wind_strength = jsonObject.optString("wind_strength");
        Sk.getInstance().wind_direction = jsonObject.optString("wind_direction");
    }

}

